package ihooyah.com.whitehat.utils

import android.graphics.Color
import android.util.Log
import com.baidu.location.BDLocation
import com.baidu.mapapi.map.*
import com.baidu.mapapi.model.LatLng
import ihooyah.com.whitehat.R
import ihooyah.com.whitehat.dao.getLocationDAO

/**
 * Created by debby on 2019/1/4.
 * 描述:
 */

/**
 * 打定位点
 */
fun locationTo(location: BDLocation?, baidu: BaiduMap) {
    //此处的BDLocation为定位结果信息类，通过它的各种get方法可获取定位相关的全部结果
    //以下只列举部分获取经纬度相关（常用）的结果信息
    //更多结果信息获取说明，请参照类参考中BDLocation类中的说明
    val latitude = location!!.latitude    //获取纬度信息
    val longitude = location.longitude    //获取经度信息
    val radius = location.radius    //获取定位精度，默认值为0.0f
    //val coorType = location.coorType
    //获取经纬度坐标类型，以LocationClientOption中设置过的坐标类型为准
    //val errorCode = location.locType
    //获取定位类型、定位错误返回码，具体信息可参照类参考中BDLocation类中的说明
    baidu.isMyLocationEnabled = true
    val localData = MyLocationData.Builder()
        .accuracy(radius)
        .direction(100f)
        .latitude(latitude)
        .longitude(longitude)
        .build()
    baidu.setMyLocationData(localData)
    baidu.animateMapStatus(
        MapStatusUpdateFactory
            .newMapStatus(
                MapStatus.Builder()
                    .target(LatLng(latitude, longitude))
                    .zoom(18f)
                    .build()
            )
    )
}


//显示轨迹列表
fun showTrajectoryList(map: BaiduMap) {
    val pointList = ArrayList<LatLng>()

    val locationList = getLocationDAO()
    for (location in locationList) {
        if (location.Latitude != 0.0 && location.Longitude != 0.0) {
            pointList.add(LatLng(location.Latitude, location.Longitude))
        }
    }
    
    if (pointList.size == 1) {
        drawMarks(map, pointList[0])
    } else {
        drawLine(map, pointList)
    }

}


/*****************
 * 绘制
 * **************/

//mark点
fun drawMarks(bdMap: BaiduMap, point: LatLng) {
    val bitmap = BitmapDescriptorFactory
        .fromResource(R.mipmap.ic_start)
    val option = MarkerOptions()
        .position(point)
        .icon(bitmap)
        .zIndex(9)
        .scaleX(1.2f)
        .scaleY(1.2f)
        .draggable(true)
    val overlay = bdMap.addOverlay(option)
    addOverlay(One, overlay)
    //调用BaiduMap对象的setOnMarkerDragListener方法设置Marker拖拽的监听
    bdMap.setOnMarkerDragListener(object : BaiduMap.OnMarkerDragListener {
        override fun onMarkerDrag(marker: Marker) {
            //拖拽中
            Log.e("marker", "拖拽中")
        }

        override fun onMarkerDragEnd(marker: Marker) {
            //拖拽结束
            Log.e("marker", "拖拽结束")
        }

        override fun onMarkerDragStart(marker: Marker) {
            //开始拖拽
            Log.e("marker", "开始拖拽")
        }
    })
}

//mark点
fun drawOneMark(bdMap: BaiduMap, point: LatLng) {
    val bitmap = BitmapDescriptorFactory
        .fromResource(R.mipmap.ic_end)
    val option = MarkerOptions()
        .position(point)
        .icon(bitmap)
        .zIndex(9)
        .scaleX(1.2f)
        .scaleY(1.2f)
        .draggable(true)
    removeOverlay(OneMark)
    val oldMark = bdMap.addOverlay(option)
    addOverlay(OneMark, oldMark)
}

//线
fun drawLine(bdMap: BaiduMap, list: ArrayList<LatLng>) {
    val colors = ArrayList<Int>()
    colors.add(Color.parseColor("#46a8ea"))
    colors.add(Color.parseColor("#fd3358"))
    colors.add(Color.parseColor("#89d173"))
    colors.add(Color.parseColor("#ece470"))
    val polyline = PolylineOptions().width(10)
        .colorsValues(colors)
        .points(list)
    removeOverlay(OneLine)
    val oldLine = bdMap.addOverlay(polyline)
    addOverlay(OneLine, oldLine)
    drawOneMark(bdMap, list[list.size - 1])
}

//清除所有绘制图层
fun clearOverlay() {
    removeAll()
}
